home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / rendering / shadeModel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.9 KB  |  162 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*  shadeModel.c  - open a window, clear the background, and render a 
  19.  *    line, a triangle fan and a quad strip using flat/smooth shading.
  20.  *
  21.  *    SPACE key    - toggle between flat/smooth shading
  22.  *    ESCAPE key    - exit program
  23.  */
  24.  
  25. #include <GL/gl.h>
  26. #include <GL/glut.h>
  27.  
  28. #include <stdio.h>
  29.  
  30. /*  Function Prototypes  */
  31.  
  32. GLvoid  initgfx( GLvoid );
  33. GLvoid    keyboard( GLubyte, GLint, GLint );
  34. GLvoid  drawScene( GLvoid );
  35.  
  36. void printHelp( char * );
  37.  
  38. /* Global Definitions */
  39.  
  40. #define KEY_ESC    27    /* ascii value for the escape key */
  41.  
  42. static GLint flatShading = GL_FALSE;
  43.  
  44. void
  45. main( int argc, char *argv[] )
  46. {
  47.     GLsizei width, height;
  48.  
  49.     glutInit( &argc, argv );
  50.  
  51.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  52.     height = glutGet( GLUT_SCREEN_HEIGHT );
  53.     glutInitWindowPosition( width / 4, height / 4 );
  54.     glutInitWindowSize( width / 2, height / 2 );
  55.     glutInitDisplayMode( GLUT_RGBA );
  56.     glutCreateWindow( argv[0] );
  57.  
  58.     initgfx();
  59.  
  60.     glutKeyboardFunc( keyboard );
  61.     glutDisplayFunc( drawScene ); 
  62.  
  63.     printHelp( argv[0] );
  64.  
  65.     glutMainLoop();
  66. }
  67.  
  68. void
  69. printHelp( char *progname )
  70. {
  71.     fprintf(stdout, "\n%s - demonstrates shading models\n\n"
  72.         "SPACE Key        - toggle between flat/smooth shading\n"
  73.         "Escape Key        - exit the program\n\n",
  74.         progname);
  75. }
  76.  
  77. GLvoid
  78. initgfx( GLvoid )
  79. {
  80.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  81.  
  82.     glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );
  83. }
  84.  
  85. GLvoid 
  86. keyboard( GLubyte key, GLint x, GLint y )
  87. {
  88.     switch (key) {
  89.     case ' ':
  90.         flatShading = !flatShading;
  91.         if (flatShading) 
  92.             glShadeModel(GL_FLAT);
  93.         else
  94.             glShadeModel(GL_SMOOTH);
  95.         glutPostRedisplay();
  96.         break;
  97.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  98.         exit(0);
  99.     }
  100. }
  101.  
  102. GLvoid
  103. drawScene( GLvoid )
  104. {
  105.     static GLfloat    blackColor[] = { 0.0, 0.0, 0.0 };
  106.     static GLfloat    redColor[]  = { 1.0, 0.0, 0.0 };
  107.     static GLfloat    purpleColor[] = { 1.0, 0.0, 1.0 };
  108.     static GLfloat    yellowColor[] = { 1.0, 1.0, 0.0 };
  109.     static GLfloat    whiteColor[] = { 1.0, 1.0, 1.0 };
  110.     static GLfloat    darkgreenColor[] = { 0.0, 0.5, 0.0 };
  111.     
  112.     glClear( GL_COLOR_BUFFER_BIT );
  113.  
  114.     /* Draw a point */
  115.     glBegin( GL_POINTS );
  116.       glColor3fv( whiteColor );
  117.       glVertex2f( 0.3, 0.85 );
  118.       glColor3f( 1.0, 0.50, 0.0 );   /* Orange */
  119.       glVertex2f( 3.0, 9.65 );       /* This point is clipped */
  120.     glEnd();
  121.  
  122.     /* Gouraud shade a line */
  123.     glBegin( GL_LINES );
  124.       glColor3f( 1.0, 0.0, 0.0 );    /* Red */
  125.       glVertex2f( 0.1, 0.1 );
  126.       glColor3fv( yellowColor );
  127.       glVertex2f( 0.2, 0.9 );
  128.     glEnd();
  129.  
  130.     /* Gouraud shade a polygon */
  131.     glBegin( GL_POLYGON );
  132.       glColor3fv( purpleColor );
  133.       glVertex2f( 0.5, 0.1 );
  134.       glColor3fv( redColor );
  135.       glVertex2f( 1.0, 0.4 );
  136.       glColor3fv( blackColor );
  137.       glVertex2f( 0.9, 1.0 );
  138.       glColor3fv( darkgreenColor );
  139.       glVertex2f( 0.3, 0.8 );
  140.       glColor3fv( whiteColor );
  141.       glVertex2f( 0.1, 0.5 );
  142.     glEnd();
  143.  
  144.     /* Draw a triangle fan */
  145.     glBegin( GL_TRIANGLE_FAN );
  146.       glColor3fv( blackColor );
  147.       glVertex2f( 0.4, 0.4 );
  148.       glColor3fv( redColor );
  149.       glVertex2f( 0.3, 0.4 );
  150.       glColor3f( 0.0, 0.0, 1.0 );    /* blue */
  151.       glVertex2f( 0.4, 0.5 );
  152.       glColor3fv( yellowColor );
  153.       glVertex2f( 0.5, 0.5 );
  154.       glColor3fv( whiteColor );
  155.       glVertex2f( 0.5, 0.4 );
  156.       glColor3fv( darkgreenColor );
  157.       glVertex2f( 0.4, 0.3 );
  158.     glEnd();
  159.  
  160.     glFlush();
  161. }
  162.